// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import WaterShader from "./WaterShader";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    @property(WaterShader)
    waterShader: WaterShader = null;

    @property(cc.Node)
    sliderGroup: cc.Node = null;

    @property(cc.Node)
    labelGroup: cc.Node = null;

    private _sliders: cc.Slider[] = [];
    private _labels: cc.Label[] = [];

    protected start(): void {
        let funcArr = [this.xSpeed, this.ySpeed, this.xRange, this.yRange];
        let valueArr = [0.52, 0.52, 0.02, 0.02];

        for (let i = 0; i < 4; i++) {
            this._sliders[i] = this.sliderGroup.children[i].getComponent(cc.Slider);
            this._labels[i] = this.labelGroup.children[i].getComponent(cc.Label);
            this._sliders[i].node.on('slide', funcArr[i], this);
            this._sliders[i].progress = valueArr[i];
            this._sliders[i].node.emit('slide');
        }
    }

    xSpeed() {
        this.waterShader.xSpeed = this._sliders[0].progress * 2 - 1;
        this._labels[0].string = "xSpeed:" + this.waterShader.xSpeed.toPrecision(4);
    }

    ySpeed() {
        this.waterShader.ySpeed = this._sliders[1].progress * 2 - 1;
        this._labels[1].string = "ySpeed:" + this.waterShader.ySpeed.toPrecision(4);
    }

    xRange() {
        this.waterShader.xRange = this._sliders[2].progress;
        this._labels[2].string = "xRange:" + this.waterShader.xRange.toPrecision(4);
    }

    yRange() {
        this.waterShader.yRange = this._sliders[3].progress;
        this._labels[3].string = "yRange:" + this.waterShader.yRange.toPrecision(4);
    }
}
